#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.testOptionController import TestOptionController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class TestOptionResource(Resource):

    # get
    @classmethod
    def get(cls, option_id=None):
        if option_id:
            kwargs = {
                'option_id': option_id
            }

            res = TestOptionController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('option_id', location='args', required=False, help='option_id参数类型不正确或缺失')
        parser.add_argument('question_id', location='args', required=False, help='question_id参数类型不正确或缺失')
        parser.add_argument('content', location='args', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('score', location='args', required=False, help='score参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = TestOptionController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    
    # delete
    @classmethod
    def delete(cls, option_id=None):
        if option_id:
            kwargs = {
                'option_id': option_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = TestOptionController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # put
    @classmethod
    def put(cls, option_id):
        if not option_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('question_id', location='form', required=False, help='question_id参数类型不正确或缺失')
        parser.add_argument('content', location='form', required=False, help='content参数类型不正确或缺失')
        parser.add_argument('score', location='form', required=False, help='score参数类型不正确或缺失')
        parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='form', required=False, help='update_time参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['option_id'] = option_id

        res = TestOptionController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # add
    @classmethod
    def post(cls):
        '''
        TestOptionList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('TestOptionList', type=str, location='form', required=False, help='TestOptionList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('TestOptionList'):
            kwargs['TestOptionList'] = json.loads(kwargs['TestOptionList'])
            for data in kwargs['TestOptionList']:
                for key in ['is_delete']:
                    data.pop(key, None)
            res = TestOptionController.add_list(**kwargs)

        else:
            parser.add_argument('question_id', location='form', required=True, help='question_id参数类型不正确或缺失')
            parser.add_argument('content', location='form', required=True, help='content参数类型不正确或缺失')
            parser.add_argument('score', location='form', required=True, help='score参数类型不正确或缺失')
            parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
            parser.add_argument('update_time', location='form', required=False, help='update_time参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = TestOptionController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
